<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入 layui.css -->
    <link href="//unpkg.com/layui@2.8.0/dist/css/layui.css" rel="stylesheet">
    <link rel="stylesheet" href="./css/link.css">
    <!-- 引入 layui.js -->
    <script src="//unpkg.com/layui@2.8.0/dist/layui.js"></script>
    <style>
        body {
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="layui-form-item">
        <button type="button" class="layui-btn" id="ID-upload-demo-btn">
            <i class="layui-icon layui-icon-upload"></i> 单图片上传
        </button>
        <div style="width: 132px;">
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="ID-upload-demo-img" style="width: 100%; height: 92px;">

            </div>
        </div>
    </div>
    <script>
        const upload = layui.upload;
        const element = layui.element;
        const layer = layui.layer;
        const $ = layui.$;
        const util = layui.util;
        const form = layui.form;
        // todo获取图片
        document.querySelector(`#ID-upload-demo-btn`)
        layui.use(function () {

            // 图片
            var uploadInst = upload.render({
                elem: '#ID-upload-demo-btn',
                url: 'https://httpbin.org/post', // 此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                before: function (obj) {
                    // 预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
                    });


                },
                done: function (res) {

                    // 上传成功的一些操作
                    // …
                    $('#ID-upload-demo-text').html(''); // 置空上传失败的状态
                },
                error: function () {
                    // 演示失败状态，并实现重传
                    // var demoText = $('#ID-upload-demo-text');
                    // demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    // demoText.find('.demo-reload').on('click', function () {
                    //     uploadInst.upload();
                    // });
                },

            });
        })

        // document.querySelector(`.add`).addEventListener(`click`, function (e) {
        //     setTimeout(function () {
        //         upload.render({
        //             elem: '#ID-test-uoload',
        //             // …
        //         });
        //     }, 2000)
        // })


    </script>
    <div class="layui-form">
        <div class="layui-input-inline">
            <input type="checkbox" name="ishot" lay-skin="switch" lay-filter="switchTest" title="是|否" class="ck1">
        </div>
    </div>
    <div class="layui-form" style="cursor: pointer;"></div>
    <div class="layui-input-inline">
        <input type="checkbox" name="ishot" lay-skin="switch" lay-filter="switchTest" title="是|否" class="ck1">
    </div>
</body>

</html>